<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 矿机管理
 */
namespace app\settle\model;

use app\settle\model\Model;
use Mscore\Core\Redis;

class Duelorder extends Model{
    
    private $orderlog = 'pro_duelorderlog';
    private $account = 'user_account';
    private $order = 'pro_duelorder';
    private $risk = 'pro_duelrisk';

    /**
     * 初始化自动连接方法
     * @param array $__construct 
     * @param array $where 
    */
    public function __construct()  
    {    
        $this->hashfix=config('redis.redis_db.db_hashfix');
    }

    /**
     * 列表
     * @param array $list 
     * @param array $where 
     */ 
    public function list($where=null,$page=1,$page_size=10){
        //矿机列表
        $list['datalist']= Model::name($this->order)
            ->alias('a')
            ->join($this->account.' b','a.aid=b.aid','left')
            ->field('a.*, b.username, b.realname')
            ->where($where)
            ->limit(($page-1)*$page_size,$page_size)
            ->order('createtime desc')
            ->select()
            ->toArray();
        //数据总数
        $list['total']=Model::name($this->order)
            ->alias('a')
            ->join($this->account.' b','a.aid=b.aid','left')
            ->where($where)
            ->count('id');
        return $list;
    }

    /**
     * 信息
     * @param array $info 
     * @param array $where 
     */ 
    public function info($id=null){  
         return Model::name($this->order)
           ->alias('a')
           ->join($this->account.' b','a.aid=b.aid','left')
           ->field('a.*, b.realname,b.mobile,b.username,b.email')
           ->where(['a.id'=>$id])
           ->find();
    }

    /**
     * 信息
     * @param array $info 
     * @param array $where 
     */ 
    public function userinfo($where=null){  
         return Model::name($this->account)
           ->where($where)
           ->value('aid');
    }

    /**
     * 总计信息
     * @param array $suminfo 
     * @param array $where 
     */ 
    public function suminfo($where=null,$field='a.*'){  
         return Model::name($this->order)
           ->alias('a')
           ->field($field)
           ->where($where)
           ->find();
    }
    
    /**
     * 信息
     * @param array $info 
     * @param array $where 
     */ 
    public function infolog($where=null,$field='*'){  
         return Model::name($this->orderlog)
           ->field($field)
           ->where($where)
           ->find();
    }


    /**
     * 保存
     * @param array $save 
     * @param array $where 
     */ 
    public function save($data=[]){
        $data  = [
            'id'=>$data['id'],
            'status'=>$data['status'],
            'update_time'=>date('Y-m-d H:i:s'),
            'language'=>$data['language']
        ];
        switch (true) {
            case isset($data['id'])&&!empty($data['id']):
                $id=$data['id']; unset($data['id']);
                Model::name($this->order)->where('id=' . $id)->update($data);
                return true;
                break;
            
            default:
                return false;
                break;
        }
    }

    /**
     * 删除
     * @param array $del 
     * @param array $where 
     */
    public function del($id=null){
     //删除
        if ($id) {
            Model::name($this->order)->where('id='.$id)->update(['dels'=>1]);
            return true;
        }
        return false;
    }

    /**
     * 獲取产品实时价格
     * @param array $getPronow 
     * @param array $where 
    */ 
    public function getprice($procode=null){ 
        $key = config('futures.daytrade');
        switch (true) {
            case !empty($procode):
                $info = Redis::exec('hget',[$this->hashfix.$key,$procode]);
                $info = isset($info)&&!empty($info)?json_decode($info,true):[];
                return $info;
                break;
            default:
                return [];
                break;
        }
    }

    /**
     * 更新订单信息
     * @param array $updateOrder
     * @param array $where $data
     */
    public function updateOrder(String $where='', Array $data=[])
    {
        Model::name($this->order)
            ->where($where)
            ->update($data);
        return true;
    }

    /**
     * 更新下单
     * @param array $addorderlog
     * @param array $id
     */
    public function updateorderlog($id,$data)
    {
        Model::name($this->orderlog)->where('orderid='.$id)->update($data);
        return true;        
    }
    /**
     * 运营控制信息
     * @param array $daytraderisk
     * @param array $where 
     */ 
    public function daytraderisk($where=null,$field='*'){
        //列表
        $info = Model::name($this->risk)
            ->field($field)
            ->where($where)
            ->find();
        return $info;
    }
    /**
     * 随机跳动点数
     * @param array $writeRedis
     * @param array $where $page $num
     */
    public function random(Array $data=[])
    {
        $rands=0;
        if(!empty($data)){
            $point_low=floatval($data['point_low']);
            $point_top=floatval($data['point_top']);
            $rands=rand(($point_low),($point_top));
            unset($point_low,$point_top,$point_rand); 
        }    
        return $rands;
    }

}
